package com.lijiajia.cloud.resource.sys.controller;

import com.lijiajia.cloud.resource.sys.service.numberid.DelegatingNumberIdService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 获取数字唯一标识
 *
 * @author lijiajia
 * @since 2019-5-27
 */
@RestController
@RequestMapping("/number-id")
public class IdController {

    private final DelegatingNumberIdService numberIdService;

    public IdController(DelegatingNumberIdService numberIdService) {
        this.numberIdService = numberIdService;
    }


    @GetMapping(value = "/{name}", params = {"!prefix", "!suffix"})
    @PreAuthorize("hasAuthority(authority.NUMBER_ID)")
    public Number get(@PathVariable String name) {
        return numberIdService.get(name);
    }


    @GetMapping(value = "/{name}")
    @PreAuthorize("hasAuthority(authority.NUMBER_ID)")
    public String get(@PathVariable String name,
                      @RequestParam(value = "prefix", required = false) String prefix,
                      @RequestParam(value = "suffix", required = false) String suffix) {
        return numberIdService.get(name, prefix, suffix);
    }

    @PutMapping(value = "/{name}")
    @PreAuthorize("hasAuthority(authority.NUMBER_ID)")
    private void set(@PathVariable String name, @RequestParam("value") Number value) {
        numberIdService.setValue(name, value);
    }
}
